<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :defaults.sync="showColumn"
               :permission="permissionList"
               :page.sync="page"
               :before-open="beforeOpen"
               @sortable-change="sortableChange"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @filter="filterChange"
               @sort-change="sortChange"
               @row-dblclick="handleRowDBLClick"               
               @on-load="onLoad">
      <template slot-scope="scope" slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-plus"
                   @click.stop="handleBatchAddComponent">添加组件
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {exportSysMicroComponentExcel,saveOrUpdateSysMicroComponent,saveOrUpdateSysMicroComponentList,saveSysMicroComponent,saveSysMicroComponentList,deleteSysMicroComponent,querySysMicroComponent,querySysMicroComponentListPage,updateSysMicroComponent,deleteSysMicroComponentList} from '@/api/micro/sysMicroComponentApi';
import {mapGetters} from 'vuex';
import { reqCommonMsg } from '@/util/reqCommonMsg';
import {filterFormat} from '@/util/util';
export default {
  data() {
    return {
 
      // 当前打开form对话框操作类型 
      operateType: 'add', 

      importDataArea: '',
      batchAddDialog: false,
      form: {},
      currentRow: {},
      selectionList: [],
      loading: true, 
      showColumn: [],
      option: {
        labelPosition: 'right',
        labelSuffix: '：',
        labelWidth: 125,
        dialogTop: '50',
        indexFixed:true, 
        stripe:true,
        calcHeight: 80,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        addBtn: false,
        viewBtn: true,
        editBtn: false,
        delBtn: false,
        cancelBtn: true,
        printBtn:false,
        saveBtn: false,
        filterBtn:true,          
        menuWidth: 100,
        dialogMenuPosition: 'center',
        dialogDirection:'rtl',
        dialogType:'dialog',
        dialogFullscreen:false,
        dialogClickModal:false,
        dialogWidth: '70%',
        highlightCurrentRow:  true,
        sortable: false,
        column: [
          {
            label: '所属应用',
            prop: 'belongAppId', 
            type: 'select', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 2000,
            overHidden: true,
            rules: [{
              message: '请输入所属应用',
              required:  true,
              trigger: 'blur'
            },
            {
              max: 50,
              message: '内容长度不能超过2000字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          {
            label: '微服务工程名',
            prop: 'microServiceCode', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 50,
            overHidden: true,
            rules: [{
              message: '请输入微服务编码',
              required:  true,
              trigger: 'blur'
            },
            {
              max: 50,
              message: '内容长度不能超过50字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          
          {
            label: '微服务名称',
            prop: 'microServiceName', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 50,
            overHidden: true,
            rules: [{
              message: '请输入微服务名称',
              required:  true,
              trigger: 'blur'
            },
            {
              max: 50,
              message: '内容长度不能超过50字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          {
            label: '组件编码',
            prop: 'componentCode', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 50,
            overHidden: true,
            rules: [{
              message: '请输入组件编码',
              required:  true,
              trigger: 'blur'
            },
            {
              max: 50,
              message: '内容长度不能超过50字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          {
            label: '组件名称',
            prop: 'componentName', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 50,
            overHidden: true,
            rules: [{
              message: '请输入组件名称',
              required:  true,
              trigger: 'blur'
            },
            {
              max: 50,
              message: '内容长度不能超过50字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          
          {
            label: '组件描述',
            prop: 'componentDesc', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: true,
            maxlength: 2000,
            overHidden: true,
            rules: [{
              message: '请输入组件描述',
              required:  false,
              trigger: 'blur'
            },
            {
              max: 2000,
              message: '内容长度不能超过2000字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          {
            label: '访问路径',
            prop: 'accessPath', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: true,
            maxlength: 500,
            overHidden: true,
            rules: [{
              message: '请输入访问路径',
              required:  false,
              trigger: 'blur'
            },
            {
              max: 500,
              message: '内容长度不能超过500字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          {
            label: '访问方式',
            prop: 'accessWay', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            filterable: true,
            span: 12,
            sortable: false,
            hide: false,
            maxlength: 30,
            overHidden: true,
            rules: [{
              message: '请输入访问方式',
              required:  false,
              trigger: 'blur'
            },
            {
              max: 30,
              message: '内容长度不能超过30字符',
              trigger: 'blur'
            }],
            dicData:[],
          },
          
          {
            labelWidth:40,
            label: '',
            prop: 'inputMsgBody',
            span: 12,
            hide:true,
            formslot:true,
          },
          {
            labelWidth:40,
            label: '',
            prop: 'outputMsgBody',
            span: 12,
            hide:true,
            formslot:true,
          }
        ]
      },
      
      data: [],
      // 请求参数区
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      reqParams: {
        singleBody: {},
        listBody: [{}],
        sqlCondition:[],
        orderRule: [],
        currentPage: 1,
        pageSize: 10
      },
      commonData: null,
      conditions: [],
      orderRules: [],
      singleBody: {}
    };
  },
  computed: {
    ...mapGetters(['website','orgTree','genOrgTree','userInfo','permission','projectInfo']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.sys_micro_component_add, false),
        viewBtn: this.vaildData(this.permission.sys_micro_component_view, false),
        delBtn: this.vaildData(this.permission.sys_micro_component_delete, false),
        editBtn: this.vaildData(this.permission.sys_micro_component_edit, false),
        printBtn: this.vaildData(this.permission.sys_micro_component_print, false),
        searchBtn:this.vaildData(this.permission.sys_micro_component_search, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push({id : ele.id});
      });
      return ids;
    } 
  },
  methods: {

    /**
     * 批量新增记录,调用父组件方法
     */
    handleBatchAddComponent(){
      this.$confirm('确定要添加组件?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$emit('handleBatchAdd', this.selectionList)
          this.selectionList = null;
        }).catch(() => {});
    },
    /**
     * 加载列表数据
     */
    onLoad(page) {
      this.loading = true;
      this.reqParams =  {
        singleBody: this.singleBody,
        listBody: [{}],
        sqlCondition: this.conditions,
        orderRule: this.orderRules,
        currentPage: page.currentPage, 
        pageSize: page.pageSize
      }
      this.commonData = reqCommonMsg(this.reqParams)
      querySysMicroComponentListPage(this.commonData).then(res => {
        const data = res.data.body;
        this.page.total = data.total;
        this.data = data.listBody;
        this.loading = false;
        this.selectionClear();
      }, error => {
        this.loading = false; 
      });
    },
    
    /**
     * 双击打开详细窗口
     */
    handleRowDBLClick (row, event) {
      if(this.permission.sys_micro_component_dbclick){
        this.$refs.crud.rowView(row,row.$index);
      }
    },
    
    /**
     * 搜索重置
     */
    searchReset() {
      this.conditions =[];
      this.singleBody = {};
      this.onLoad(this.page);
    },
    /**
     * 搜索
     */
    searchChange(params, done) {
      this.singleBody = params
      this.page.currentPage = 1;
      this.onLoad(this.page);
      done();
    },
    refreshChange(){
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    
    /**
     * 打开编辑、查询弹窗前查询记录
     */  
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        this.operateType = 'edit';
        this.reqParams =  {
          singleBody: {
            id:this.form.id
          }
        }
        let commonData = reqCommonMsg(this.reqParams)
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading'
        });
        querySysMicroComponent(commonData).then(res => {
          this.form = res.data.body.singleBody;
          this.form.inputMsgBody  = JSON.parse(this.form.inputMsgBody);
          this.form.outputMsgBody = JSON.parse(this.form.outputMsgBody);
          this.inputData = this.form.inputMsgBody;
          this.outputData = this.form.outputMsgBody;
          loading.close();
          done();
        }, error => { 
          loading.close();
          done();
        });
      }
      if (['add'].includes(type)) {
        this.inputData = [];
        this.outputData = [];
        this.operateType = 'add';
        done();
      }
    },
    /**
     * 翻页查询
     */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    /**
     * 更改每页条数
     */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /**
     * 自定义查询条件
     */
    filterChange(result) {
      this.page.currentPage = 1;
      this.conditions = filterFormat(result)
      this.onLoad(this.page);
    },
    /**
     * 列表排序
     */
    sortChange(val){
      this.orderRules=val
      this.onLoad(this.page);
    },
  
    
    /**
     * 获取业务字典参数
     */
    async getSysDict() {
      let dicData = await this.$params.getSysDict('["belongAppId"]');
      this.findObject(this.option.column, 'belongAppId').dicData = dicData.belongAppId;
    },

  },
  created() {
    this.showColumn = this.$getStore({name: this.userInfo.userId+'sysMicroComponentColumnDialog'});
    //获取业务字典
    setTimeout(() => {
      this.getSysDict();
    }, 1000);
  },
  watch: {
    showColumn: {
      handler(newValue, oldValue) {
        this.$setStore({name: this.userInfo.userId+'sysMicroComponentColumnDialog', content: this.showColumn});
      },
      deep: true
    }
  }
};
</script>
